<div class="page">
	<div class="page-header">
		<div class="pull-left">
			<i class="icon-awesome-file-alt icon-large"></i>
			<span class="divider"></span>
			<div class="title">
				<h3><?php echo __('Projetos');?></h3>
				<small><?php echo ('Gerencie todos os projetos');?></small>
			</div>
		</div>
		<div class="pull-right">
			<?php echo $this->Html->link('<i class="icon-awesome-plus icon-white"></i>'.__('Novo'), array('action' => 'add'), array('escape' => false, 'class' => 'btn btn-primary btn-square', 'id' => 'btn-add-projeto')); ?>
		</div>
	</div>
	<div class="page-content">
		<?php echo $this->Form->create('Filter', array('class' => 'well')); ?>
		<div class="control-group">
			<div class="controls">
				<div class="input-append">
					<?php echo $this->Form->input('palavra_chave', array('label' => false, 'div' => false, 'class' => 'span12','placeholder' => 'Digite a palavra-chave')); ?>
					<?php echo $this->Form->button('<i class="icon-search icon-white"></i> Pesquisar', array('type' => 'submit', 'div' => false, 'class' => 'btn btn-primary', 'escape' => false)); ?>
				</div>
			</div>
		</div>
		<?php echo $this->Form->end();?>
		<table class="table table-bordered table-striped table-condensed">
		<thead>
		<tr>
			<th><?php echo $this->Paginator->sort('id');?></th>
			<th><?php echo $this->Paginator->sort('nome');?></th>
			<th class="actions"><?php echo __('Actions');?></th>
		</tr>
		</thead>
		<tbody>
<?php
		foreach ($projetos as $projeto): ?>
		<tr>
			<td><?php echo h($projeto['Projeto']['id']); ?>&nbsp;</td>
			<td><?php echo h($projeto['Projeto']['nome']); ?>&nbsp;</td>
			<td class="actions">
				<?php echo $this->Html->link('<i class="icon-eye-open icon-white"></i>', array('action' => 'view', $projeto['Projeto']['id']), array('escape' => false, 'class' => 'btn btn-primary btn-mini')); ?>
				<?php echo $this->Html->link('<i class="icon-pencil icon-white"></i>', array('action' => 'edit', $projeto['Projeto']['id']), array('escape' => false, 'class' => 'btn btn-primary btn-mini')); ?>
				<?php echo $this->Form->postLink('<i class="icon-trash icon-white"></i>', array('action' => 'delete', $projeto['Projeto']['id']), array('escape' => false, 'class' => 'btn btn-danger btn-mini'), __('Are you sure you want to delete # %s?', $projeto['Projeto']['id'])); ?>
			</td>
		</tr>
<?php endforeach; ?>
		</tbody>
		</table>
		
		<div class="pull-left">
			<?php echo $this->Paginator->counter(array('format' => __('Página {:page} de {:pages}, exibindo {:current} do total de {:count} registros'))); ?>
		</div>
		<div class="pagination pagination-right pagination-small text-right">
			<ul>
			<?php
				echo $this->Paginator->first(('<i class="icon-awesome-step-backward"></i>'), array('tag' => 'li', 'escape' => false));
				echo  ($this->Paginator->hasPrev()) ? $this->Paginator->prev(('<i class="icon-awesome-backward"></i>'), array('tag' => 'li', 'escape' => false), null, array('class' => 'disabled', 'tag' => 'li', 'escape' => false, 'disabledTag' => 'a')) : '';
				echo $this->Paginator->numbers(array('separator' => '', 'tag' => 'li', 'currentClass' => 'active', 'currentTag' => 'a'));
				echo ($this->Paginator->hasNext()) ? $this->Paginator->next(('<i class="icon-awesome-forward"></i>'), array('tag' => 'li', 'escape' => false), null, array('class' => 'disabled', 'tag' => 'li', 'escape' => false, 'disabledTag' => 'a')) : '';
				echo $this->Paginator->last(('<i class="icon-awesome-step-forward"></i>'), array('tag' => 'li', 'escape' => false));
			?>
			</ul>
		</div>
		
	</div>
</div>

<div id="modal-projeto" class="modal hide fade">
	<div class="modal-header">
		<?php echo $this->Form->button('×', array('type' => 'button', 'div' => false, 'class' => 'close', 'data-dismiss' => 'modal')); ?>
		<h4>Novo Projeto</h4>
	</div>
	<div class="modal-body">
	</div>
	<div class="modal-footer">
		<?php echo $this->Form->button('<i class="icon-awesome-remove icon-white"></i>&nbsp;<span>' . __('Cancelar') . '</span>', array('div' => false, 'class' => 'btn', 'data-dismiss' => 'modal'));?>		
		<?php echo $this->Form->button('<i class="icon-awesome-save icon-white"></i>&nbsp;<span>' . __('Salvar') . '</span>', array('div' => false, 'class' => 'btn btn-primary', 'id' => 'btn-save-projeto')); 	?>
	</div>
</div>

<script type="text/javascript">

	$(document).ready(function () {
		$('.page-header a.btn-primary, .actions a.btn-primary').click(function(event) {
			event.preventDefault();
			$("#modal-projeto .modal-body").empty();
			$("#ajax-indicator").show();
			var url = $(this).attr('href');
            $.get(url, function (data) {
				if (url.indexOf('add') >= 0) {
					$("#modal-projeto .modal-header h4:first").text('Novo Projeto');
					$("#modal-projeto .modal-footer button:last-child").show();
				} else if (url.indexOf('edit') >= 0) {
					$("#modal-projeto .modal-header h4:first").text('Editar Projeto');
					$("#modal-projeto .modal-footer button:last-child").show();
				} else if (url.indexOf('view') >= 0) {
					$("#modal-projeto .modal-header h4:first").text('Visualizar Projeto');
	    			$("#modal-projeto .modal-footer button:last-child").hide();
				}
				$("#modal-projeto .modal-body").html(data);
                $("#modal-projeto").modal();  
            })
            .always(function(){
            	$("#ajax-indicator").hide();
            });

    		$('#btn-save-projeto').click(function(event){
    			event.preventDefault();
    			var button = $(this);
    			var cloneButton = button.clone(true);
    			var contentModal =  $("#modal-projeto .modal-body");
    			var form = contentModal.children('form');
    			
    			$("#ajax-indicator").show();
    			button.addClass('disabled').children('span').text('Salvando...').attr({disabled: 'disabled'});
    		    $.post(form.attr('action'), form.serialize(), function (response) {
    		        if ($(response).find('.error').length) {
    		        	contentModal.html(response);
    		        	button.replaceWith(cloneButton);
    		        } else {
    		           location.reload();
    		        }
    		    })
    		    .always(function(){
    		    	$("#ajax-indicator").hide();
    		    });
    		});
            
		});
	});

</script>